﻿

//------------public-begin--------------//

function isNull(obj, msg) {
	if(!verifyNotNull(obj.value)) {
		alert("请输入" + msg);
		obj.focus();
		return false;
	}
	return true;
}

function verifyNotNull(string){
	if(string == null) return false;
	
	string = trim(string);
	if(string == "") return false;

	return true;
}

//判断日期是否小于等于系统日期
//the parameter "date" formated with "YYYYMMDD"
function compareDateWithSys(date) {
	var sysdate = getSysDate("");
	if(eval(date) <= eval(sysdate))
		return true;
	else
		return false;
}

//判断日期是否小于等于系统日期
//the parameter "date" formated with "YYYYMMDD"
function compareDateWithSys1(date) {
	var sysdate = getSysDate("");
	if(eval(date) < eval(sysdate))
		return true;
	else
		return false;
}


function verifyMoreZero(string) {
	var n = new Number(str);
	if(isNaN(n)) {
		return false;
	}
	else {
		if(str.indexOf("e") != -1 || str.indexOf("E") != -1)
			return false;
		else if(n <= 0) {
			return false;
		}
	}
	return true;
}

//验证数字(只对>0的数据适合)(v1.01)
function verifyNum(obj, message, maxDigitLength, maxDecimalLength, isMoreZero) {
	//*********************************************
	//obj				待验证的控件(文本框)
	//message			控件(文本框)代表字段的名称	
	//maxDigitLength	最大整数部分长度
	//maxDecimalLength	最大小数部分长度
	//isMoreZero		是否大于0
	//********************************************

	var result=false;
	if(obj.value.substring(obj.value.length-1,obj.value.length) == ".")
		obj.value = obj.value + "0";
	if(obj.value.indexOf(" ") != -1) {
		alert(message + "输入格式错误");
		obj.focus();
		return false;
	}
	var str = obj.value;

	if(str == "")  {
		alert(message + "不能为空");
		obj.focus();
		return false;
	}
	
	var n = new Number(str);
	if(isNaN(n)) {
		alert(message + "必须是数字");
	}
	else {
		if(str.indexOf("e") != -1 || str.indexOf("E") != -1)
			alert(message + "必须是数字");
		else if(n <= 0) {
				if(n < 0) {
					alert(message + "必须大于0");
				}
				else {
					if(n == 0 && isMoreZero) {
						alert(message + "必须大于0");
					}
					else
						return true;
				}
		}
		else {
			var digLen = 0;
			var decLen = 0;
			if(obj.value.indexOf(".") == -1) {
				digLen = str.length;
				decLen = 0;
			}
			else {
				digLen = str.indexOf(".");
				decLen = str.length - digLen - 1;
			}
			if(digLen > maxDigitLength || decLen > maxDecimalLength) 
					if(maxDecimalLength == 0) 
						alert("[" + message + "]的输入是整数，最多可以" + maxDigitLength + "位");
					else
						alert("[" + message + "]整数最多可以输入" + maxDigitLength + "位，小数为" + maxDecimalLength + "位");
			else
				result = true;
		}
	}
	obj.focus();
	return result;
}

//检验日期
function verifyDate(yearObj, monthObj, dayObj) {
	//*********************************************
	//yearObj			年(文本框)
	//monthObj			月(文本框)
	//dayObj			日(文本框)
	//********************************************
	var syear = yearObj.value;
	var smonth = monthObj.value;
	var sday = dayObj.value;
	var pyear = /[0-9]{4}/;
	var pmonth = /[0-9]{2}/;
	var result = false;
  
	if(!pyear.test(syear) || syear.length != 4) {
		alert("日期格式错误,请重新输入");
		yearObj.focus();
		return false;
	}
	if(!pmonth.test(smonth) || smonth.length != 2) {
		alert("日期格式错误,请重新输入");
		monthObj.focus();
		return false;
	}
	if(!pmonth.test(sday) || sday.length != 2) {
		alert("日期格式错误,请重新输入");
		dayObj.focus();
		return false;
	}
	var tmpYear = new Number(yearObj.value);
	var tmpMonth = new Number(monthObj.value);
	var tmpDay = new Number(dayObj.value);
	if(tmpYear < 1970) {
		alert("日期必须大于1970年");
		yearObj.focus();
		return false;
	}
	var dt = new Date(tmpYear, tmpMonth - 1, tmpDay);
	if(dt.getMonth() + 1 != tmpMonth || dt.getDate() != tmpDay) {
		alert("日期格式错误,请重新输入");
		if(dt.getMonth() + 1 != tmpMonth)
		monthObj.focus();
	else
		dayObj.focus();
		return false;
	}
	return true;
}

//获取当前日期,填入文本框
function genarateDate(yearObj, monthObj, dayObj) {
	var d1 = new Date();
	yearObj.value = d1.getYear();
	if(d1.getMonth() + 1 < 10) {
		monthObj.value = "0" + (d1.getMonth() + 1);
	}
	else {
		monthObj.value = d1.getMonth() + 1;
	}
	if(d1.getDate() < 10) {
		dayObj.value = "0" + d1.getDate();
	}
	else {
		dayObj.value = d1.getDate();
	}
}

//------------public--end----------------//




//------------private--begin----------------//

//============================================
//delete the space of strparm in the side
//============================================
function trim(strparm) {
	var strleng;
  	strlength=strparm.length;

  	if (strlength==0) {
	    return "";
	}

  	var leftpos,rightpos;
  	for (var i=0;i<strlength;i++) {
    	if (strparm.charAt(i)!=" ") {
      		leftpos=i;
      		break;
    	}
  	}

  	for (i=0;i<strlength;i++) {
    	if (strparm.charAt(strlength-i-1)!=" ") {
      		rightpos=strlength-i-1;
      		break;
    	}
  	}

  	var strtemp;
  	strtemp="";
  	for (i=leftpos;i<rightpos+1;i++) {
    	strtemp=strtemp+strparm.charAt(i);
  	}

  	return strtemp;
}

function len(checkString) {
	return checkString.length;
}

//Get current system date
function getSysDate(seprator) {
	if(seprator == null) seprator = "";
	
	var date = new Date();
	var year = "1970";
	var month = "01";
	var day = "01";

	year = date.getYear();
	
	if(date.getMonth() + 1 < 10) {
		month = "0" + (date.getMonth() + 1);
	}
	else {
		month = date.getMonth() + 1;
	}

	if(date.getDate() < 10) {
		day = "0" + date.getDate();
	}
	else {
		day = date.getDate();
	}
	
	return year + seprator + month + seprator + day;
}

